import Vue from 'vue'
import Vuex from 'vuex'
import { log } from 'util';
import req from "@/utils/req.js";
let getSource = req.getSource;

Vue.use(Vuex)

//给自己的备注 , 当名字是三个单词时最后一个单词开头大写
export default new Vuex.Store({
  state: {
    // 是否在顶级路由,如果是则启用侧边栏
    usesideBar: true,
    // 侧边栏是否打开了
    opensideBar: false,
    // 是否打开播放器
    openplayer: false,
    //播放状态
    audiostate: false,
    //当前时间
    currenttime: 0,
    //播放时长:
    duration: 0,
    //当前歌曲在列表中的index
    curindex: -1,
    //当前播放列表
    curplayList: [],
    //是否打开播放列表
    openplayList: false,
    //播放模式
    playmode: 0,
    //歌曲详情
    songinfoDetails: null,
    playid: null,
    lyric: null,
    highline:1,
  },
  mutations: {
    //是否打开侧边栏
    openSideBar(state, val) {
      state.opensideBar = val
    },
    //当前页面是否需要使用侧边栏，原则上只有一级路由页面才使用侧边栏
    useSideBar(state, val) {
      state.usesideBar = val
    },
    // 是否打开播放器
    openPlayer(state, val) {
      state.openplayer = val
    },
    // 歌曲的播放状态
    audioState(state, val) {
      state.audiostate = val
    },
    // 歌曲当前的进度时间
    currenttime(state, val) {
      state.currenttime = val
    },
    // 歌曲的时长
    duration(state, val) {
      state.duration = val
    },
    // 当前的下标
    curIndex(state, val) {
      state.curindex = val;
    },
    // 接收 next 或 pre 控制下一首或上一首
    changeIndex(state, val) {
      let index = -1;
      //如果是下一首
      if (val == 'next') {
        //单曲循环
        if (state.playmode == 1) {
          index = state.curindex;
          //随机播放
        } else if (state.playmode == 2) {
          function getrandom(min, max) {
            //包含min 和 max
            return Math.floor(Math.random() * (max - min + 1)) + min;
          }
          index = getrandom(0, state.curplayList.length - 1)
          //列表循环
        } else {
          if (state.curindex == state.curplayList.length - 1) {
            index = 0;
          } else {
            index = state.curindex + 1;
          }
        }
      }
      // 如果是上一首
      if (val == 'pre') {
        //单曲循环
        if (state.playmode == 1) {
          index = state.curindex;
          //随机播放
        } else if (state.playmode == 2) {
          index = state.curindex - 1;
          //列表循环
        } else {
          if (state.curindex == 0) {
            index = state.curplayList.length - 1;
          } else {
            index = state.curindex - 1;
          }
        }
      }
      state.curindex = index;
    },
    //当前播放列表
    curPlayList(state, val) {
      state.curplayList = val
    },
    //是否打开播放列表
    openPlayList(state, val) {
      state.openplayList = val
    },
    // 切换模式
    playMode(state, val) {
      state.playmode += 1;
      if (state.playmode > 2) {
        state.playmode = 0;
      }
    },
    //从播放列表中删除选中项
    delCur(state, val) {
      state.curplayList.splice(val, 1)
      if (val == state.curindex - 1) {
        state.curindex -= 1
      }
      else if (val < state.curindex) {
        state.curindex -= 1
      }
    },
    //歌曲详情
    songInfoDetails(state, val) {
      state.songinfoDetails = val
    },
    getlyric(state, val) {
      state.lyric = val
    },
    highline(state, val) { 
      state.highline = val
    },
    playid(state, val) {
      state.playid = val
    },
    addCurList(state, val) {
      state.curplayList.splice(state.curindex + 1, 0, val)
    }


  },
  getters: {
    //监听播放歌曲的下标
    playdetails: (state) => {
      return state.curplayList.length > 0 && state.curplayList[state.curindex];
    },

    // songListFormat: (state) => {
    //   state.curplayList.forEach(song => { 
    //     let str = '';
    //     song.ar.forEach((ar, index) => { 
    //       if (index != 0) { 

    //       }
    //     })
    //   })
    // }
  },
  actions: {
    getSongdetails(context, id) {
      let songdetail = {
        url: '/song/detail',
        method: 'get',
        //此处传入歌曲ids
        params: {
          ids: id
        }
      };
      let lyric = {
        url: '/lyric',
        method: 'get',
        //此处传入歌曲ids
        params: {
          id: id
        }
      };
      getSource(songdetail).then(res => {
        console.log('songdetail', res)
        context.commit('songInfoDetails', res.data.songs[0])
      })
      getSource(lyric).then(res => {
        console.log('lyric', res)
        if (!res.data.lrc) {
          context.commit('getlyric', '暂无歌词')
        } else {
          context.commit('getlyric', res.data.lrc.lyric)
        }

      })
    },
  }
})
